package com.macro.mall.portal.service.impl;

import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.api.ResultCode;
import com.macro.mall.common.exception.Asserts;
import com.macro.mall.common.utils.Assist;
import com.macro.mall.mapper.UmsShippingAddMapper;
import com.macro.mall.model.UmsMember;
import com.macro.mall.model.UmsShippingAdd;
import com.macro.mall.portal.service.UmsMemberService;
import com.macro.mall.portal.service.UmsShippingAddService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * UmsShippingAdd的服务接口的实现类
 *
 * @author gaodx
 */
@Slf4j
@Service
public class UmsShippingAddServiceImpl implements UmsShippingAddService {

    private boolean isDebugEnabled;

    @Autowired
    private UmsShippingAddMapper umsShippingAddMapper;
    @Autowired
    private UmsMemberService umsMemberService;
    // TODO 当你看到这个方法时你应该创建一个工具类做通用的方法,定义自己的返回格式化

    @Override
    public List<UmsShippingAdd> find(UmsShippingAdd value) {
        //TODO这里可以做通过Assist做添加查询
        List<UmsShippingAdd> result = null;
        Assist assist = new Assist();

        assist.andEq("uese_id", value.getUeseId());
        assist.andEq("status","01");
        try {
            result = umsShippingAddMapper.selectUmsShippingAdd(assist);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行获取UmsShippingAdd数据集-->结果:", result);
        }
        return result;
    }

    @Override
    public UmsShippingAdd findOne(Integer id) {
        if (id == null) {
            if (isDebugEnabled) {
                log.debug("执行通过UmsShippingAdd的id获得UmsShippingAdd对象-->失败:id不能为空");
            }
            Asserts.fail("执行通过UmsShippingAdd的id获得UmsShippingAdd对象-->失败:id不能为空");
        }
        UmsShippingAdd result = null;
        try {
            result = umsShippingAddMapper.selectUmsShippingAddById(id);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行通过UmsShippingAdd的id获得UmsShippingAdd对象-->结果:", result);
        }
        return result;
    }

    @Override
    public CommonResult saveNotNull(UmsShippingAdd value) {
        if (value == null) {
            if (isDebugEnabled) {
                log.debug("执行将UmsShippingAdd中属性值不为null的数据保存到数据库-->失败:对象不能为空");
            }
            Asserts.fail("失败:对象不能为空");
        }
        int result = 0;
        try {
            result = umsShippingAddMapper.insertNotNullUmsShippingAdd(value);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行将UmsShippingAdd中属性值不为null的数据保存到数据库-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult updateNotNullById(UmsShippingAdd value) {
        if (value == null) {
            if (isDebugEnabled) {
                log.debug("执行通过UmsShippingAdd的id更新UmsShippingAdd中属性不为null的数据-->失败:对象为null");
            }
            Asserts.fail("执行通过UmsShippingAdd的id更新UmsShippingAdd中属性不为null的数据-->失败:对象为null");
        }
        int result = 0;
        try {
            if("true".equals(value.getDefaultStatus())) {
                UmsMember currentMember = umsMemberService.getCurrentMember();
                UmsShippingAdd aFalse = UmsShippingAdd.builder().defaultStatus("false").build();
                Assist assist = new Assist();
                assist.andEq("uese_id",currentMember.getId());
                umsShippingAddMapper.updateNotNullUmsShippingAdd(aFalse,assist);
            }
            result = umsShippingAddMapper.updateNotNullUmsShippingAddById(value);

        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行通过UmsShippingAdd的id更新UmsShippingAdd中属性不为null的数据-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult deleteById(Integer id) {
        if (id == null) {
            if (isDebugEnabled) {
                log.debug("执行通过UmsShippingAdd的id删除UmsShippingAdd-->失败:id不能为空");
            }
            Asserts.fail("执行通过UmsShippingAdd的id删除UmsShippingAdd-->失败:id不能为空");
        }
        int result = 0;
        try {
            result = umsShippingAddMapper.deleteUmsShippingAddById(id);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行通过UmsShippingAdd的id删除UmsShippingAdd-->结果:", result);
        }
        return CommonResult.success(null);
    }


}